package com.xxwu.springblogsystem.config;

import com.xxwu.springblogsystem.Contants;
import com.xxwu.springblogsystem.utils.JWTUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
@Slf4j
public class LoginIntercepter implements HandlerInterceptor {
    /**
     * 获取token
     * 检验token
     * 验证通过
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String method = request.getMethod();
        if(method.equals("OPTIONS")){
            return true;
        }
        String token = request.getHeader("user_token");
        log.info("获得token, token:"+token);
        boolean claims = JWTUtils.checkToken(token);
        if(claims){
            log.info("token解析结果为：" + claims);
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
